package fiarproject;

/**
 * Created by IntelliJ IDEA.
 * <p/>
 * Date: 31/08/11
 * Time: 17:28
 */

import java.util.ArrayList;

/***
     * Class holds Pair objects containing x,y co-ordinates of winning rows
     */
    public class WinningRow {
        private final Pair[] row = new Pair[4];

        /***
         * Constructor adds winning pairs to row object
         * @param p0 1st x,y co-ordinates of fiar
         * @param p1 2nd x,y co-ordinates of fiar
         * @param p2 3rd x,y co-ordinates of fiar
         * @param p3 4th x,y co-ordinates of fiar
         */
        public WinningRow(Pair p0, Pair p1, Pair p2, Pair p3) {
            row[0] = p0;
            row[1] = p1;
            row[2] = p2;
            row[3] = p3;
        }

        /***
         * Returns a pair object of a given position in the row
         * @param i Position of pair in row to be returned
         * @return Pair object of piece position
         */
        public Pair getPair(int i) {
            return row[i];
        }

        /***
         * Returns true if r has 2 or more co-ordinates shared with this row object
         * @param r row to compare with
         * @return true if there are more than 1 common pairs
         */
        public boolean equals(WinningRow r) {
            int commonPairs = 0;
            for(int i =0; i < 4; i ++) {
                for(int j =0; j < 4; j++) {
                if(getPair(i).equals(r.getPair(j))) commonPairs++;
                }
                }
            if(commonPairs > 1) return true;
            return false;
        }

        /***
         * Uses equals method to check if the winning row has already been entered, or is not valid as it contains
         * over 1 pair in common with another winning row.
         * @param a ArrayList of either black or white winning rows
         * @return true if win has already been counted
         */
        public boolean isIn(ArrayList<WinningRow> a) {
            for(WinningRow r: a) {
                if(equals(r)) return true;
            }
            return false;
        }
    }
